<?php
/*
 * IKPHP 爱客开源社区 @copyright (c) 2012-3000 IKPHP All Rights Reserved 
 * @Author 小麦
 * @Email:ikphp@sina.cn
 * @Description:用户
 */
class controller_user extends controller_frontend {
	public function _init() {
		parent::_init ();
		$this->user_mod = D ( 'common/user' );
	}
	public function login($email = '', $password = '', $verify = '') {
		if (IS_POST) {
			if(I('post.source') != 'index_nav'){
				/* 检测验证码 */
				if(!check_verify($verify)){
					$this->error('验证码输入错误！');
				}				
			}
			/* 验证邮箱 */
			if (empty ( $email ) || empty ( $password )) {
				return $this->error('Email、密码不能为空！');
			}
			if (! preg_match ( '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/', $email )) {
				return $this->error('邮箱格式不正确！');
			}
			
			/* 调用UC登录接口登录 */
			import ( 'api_user', 'user/api/' );
			$user = new api_user ();
			$uid = $user->login ( $email, $password );
			
			if (0 < $uid) {
				/* 登录用户 */
				$Member = D ( 'common/user' );
				if ($Member->login ( $uid )) { //登录用户
					// 跳转到登陆前页面（执行同步操作）
					$ret_url = I ( 'post.ret_url', '', 'trim' );
					if (empty ( $ret_url )) {
						$this->redirect ( 'home/index/index' );
					}
					$this->redirect ( $ret_url );
				} else {
					$this->error ( $Member->getError () );
				}
			
			} else { //登录失败
				switch ($uid) {
					case - 1 :
						$error = '用户不存在或被禁用！';
						break; //系统级别禁用
					case - 2 :
						$error = '密码错误！';
						break;
					default :
						$error = '未知错误！';
						break; // 0-接口参数错误（调试阶段使用）
				}
				$this->error ( $error );
			}
		
		} else {
			$this->visitor && $this->redirect ( 'space/index/index', array ('id' => $this->visitor ['doname'] ) );
			// 来路
			if (isset ( $_SERVER ['HTTP_REFERER'] ) && strpos ( $_SERVER ['HTTP_REFERER'], 'register' ) == false && strpos ( $_SERVER ['HTTP_REFERER'], 'logout' ) == false) {
				$ret_url = $_SERVER ['HTTP_REFERER'];
			} else {
				$ret_url = C ( 'ik_site_url' );
			}
			$this->assign ( 'ret_url', $ret_url );
			
			$this->_seo ( array ('title' => '会员登录' ) );
			$this->display ();
		}
	}
	/* 验证码，用于登录和注册 */
	public function captcha() {
		$verify = new verify ( array ('imageH' => 50, 'imageW' => 255 ) );
		$verify->entry ( 1 );
	}
	/**
	 * 用户退出
	 */
	public function logout() {
		if(is_login()){
			$this->user_mod->logout();
			$this->success('退出成功！', U('user/login'));
		} else {
			$this->redirect('user/login');
		}
	}
	//2014年3月17日修改用户注册 增加了新的ikmd5验证
	public function register($username = '', $password = '', $repassword = '', $email = '', $authcode = ''){
		$this->visitor && $this->redirect ( 'space/index/index', array ('id' => $this->visitor['doname']));	
			
	    if(C('ik_isinvite') == '2'){
            $this->error('注册已关闭');
        }
        if(IS_POST){ 
        	//弹出 快捷注册
			$simple = I( 'get.simple', 0, 'trim,intval');
			
        	//注册用户
        	/* 检测验证码 */
/*			if(!check_verify($authcode)){
				$this->error('验证码输入错误！');
			}*/			
			/* 检测密码 */
            //简单注册没有二次密码
			if($simple == 0){
				if ($password != $repassword) {
					$this->error('密码和重复密码不一致！');
				}
			}						
        	/* 验证邮箱 */
			if( !preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$email) ){
				return $this->error('邮箱格式不正确！');
			}
        	if(empty($email) || empty($username) || empty($password)){ 
				return $this->error('Email、用户名、密码不能为空！');
			}
						
			/* 调用注册接口注册用户 */
			import ( 'api_user', 'user/api/' );		
            $user = new api_user(); 
			$uid = $user->register($username, $password, $email);
			if(0 < $uid){ //注册成功
				//TODO: 发送验证邮件
				$this->success('注册成功！',U('login'));
			} else { //注册失败，显示错误信息
				$this->error($this->showRegError($uid));
			} 
       	
        }else{
        	$this->_seo ( array (
				'title' => '会员注册'
			) );
        	$this->display();
        }
	}
	/**
	 * 检测用户
	 */
	public function checkUser() {
		$type = I( 'get.t' );
		/* 调用UC登录接口登录 */
		import ( 'api_user', 'user/api/' );		
		$user_mod = new api_user();
		switch ($type) {
			case 'email' :
				$email = I ( 'email' );
				echo $user_mod->email_exists ( $email ) ? 'false' : 'true';
				break;
			
			case 'username' :
				$username = I ( 'username', '', 'trim' );
				echo $user_mod->username_exists ( $username ) ? 'false' : 'true';
				break;
		}
	}
	/**
	 * 获取用户注册错误信息
	 * @param  integer $code 错误编码
	 * @return string        错误信息
	 */
	private function showRegError($code = 0){
		switch ($code) {
			case -1:  $error = '用户名长度必须在16个字符以内！'; break;
			case -2:  $error = '用户名被禁止注册！'; break;
			case -3:  $error = '用户名被占用！'; break;
			case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
			case -5:  $error = '邮箱格式不正确！'; break;
			case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
			case -7:  $error = '邮箱被禁止注册！'; break;
			case -8:  $error = '邮箱被占用！'; break;
			case -9:  $error = '手机格式不正确！'; break;
			case -10: $error = '手机被禁止注册！'; break;
			case -11: $error = '手机号被占用！'; break;
			case -12: $error = '个性域名长度不合法！'; break;
			case -13: $error = '个性域名禁止注册！'; break;
			case -14: $error = '个性域名被占用！'; break;
			default:  $error = '未知错误';
		}
		return $error;
	}				
}